stringFuncs.seperateLettersDigits   C
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 18
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 31
rs 5.9999

How to fix   Complexity   

Complexity

Complex classes like stringFuncs.seperateLettersDigits often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/** global: UB */
2
3
var stringFuncs = {
4
	
5
	// converts
6
	toInt: function(){
7
		var text = this;
8
		if (text == null || text == "") {
9
			return 0;
10
		}
11
		
12
		// if hex
13
		if (text.substr(0, 2) == "0x") {
14
			return parseInt("0x" + text.substr(2).toUpperCase());
15
		}
16
		if (text.charAt(0) == "#") {
17
			return parseInt("0x" + text.substr(1).toUpperCase());
18
		}
19
		
20
		// if decimal
21
		return parseInt(text);
22
	},
23
	toNumber: function(hasCommas = false){
24
		var text = this;
25
		
26
		// return 0 if null
27
		if (text == null || text == "") {
28
			return 0;
29
		}
30
		
31
		if (hasCommas) {
32
			text = text.mergeNumCommas();
33
		}
34
		
35
		var num = parseFloat(text);
36
		
37
		// return 0 if NaN
38
		if (num != num) {
39
			return 0;
40
		}
41
		
42
		return num;
43
	},
44
	toBoolean: function(){
45
		var text = this;
46
		if (text == null) {
47
			return false;
48
		}
49
		text = text.toLowerCase();
50
		return text == "1" || text == "true" || text == "yes" || text == "y";
51
	},
52
	
53
	
54
	// parsing
55
	/** merge by comma if "1,500,000" becomes "1500000" */
56
	mergeNumCommas: function(){
57
		var textStr = this;
58
		
59
		// create common lookups
60
		if (UB.isNumericDict == null) {
61
			UB.isNumericDict = { };
62
			for (var n = (0);n<10;n++){
63
				UB.isNumericDict[n] = true;
64
			}
65
		}
66
		
67
		if (textStr.indexOf(",") > -1) {
68
			var sepTextStr = "";
69
			for (var c = (0), cl = (textStr.length);c<cl;c = (c + 1)){
70
				var char = textStr.charAt(c);
71
				
72
				// if prev is number, this is "," and next is number
73
				if (!(UB.isNumericDict[textStr.charAt(c - 1)] && char == "," && UB.isNumericDict[textStr.charAt(c + 1)])) {
74
					sepTextStr += char;
75
				}
76
			}
77
			return sepTextStr;
78
		}
79
		return textStr;
80
	},
81
	seperateLettersDigits: function(){
82
		var text = this;
83
		
84
		// create common lookups
85
		if (UB.isNumericDict == null) {
86
			UB.isNumericDict = { };
87
			for (var n = (0);n<10;n++){
88
				UB.isNumericDict[n] = true;
89
			}
90
		}
91
		
92
		var wasDigit = false;
93
		var wasChar = "";
94
		var isDigit;
95
		var sepTextStr2 = "";
96
		
97
		for (var c = (0), cl = (text.length);c<cl;c = (c + 1)){
98
			var char = text.charAt(c);
99
			
100
			// if last is digit & this is char, or vice-versa
101
			if ((isDigit = (UB.isNumericDict[char] == true)) != wasDigit && c > 0) {
102
				if (wasChar != " " && char != " " && wasChar != "." && char != ".") {
103
					sepTextStr2 += " ";
104
				}
105
			}
106
			wasDigit = isDigit;
107
			sepTextStr2 += (wasChar = char);
108
		}
109
		
110
		return sepTextStr2;
111
	},
112
	seperateByMany: function(seperateBy, joinBy = null){
113
		var sepTextStr = this;
114
		for (var s = (0), sl = (seperateBy.length);s<sl;s = (s + 1)){
115
			
116
			// if seperator found split by it
117
			while (sepTextStr.indexOf(seperateBy[s]) > -1) {
118
				sepTextStr = sepTextStr.split(seperateBy[s]).join(joinBy ? joinBy[s] : " ");
119
			}
120
		}
121
		return sepTextStr;
122
	},
123
	splitLettersDigits: function(){
124
		var text = this;
125
		return text.seperateLettersDigits().removeMultiSpaces().split(" ");
126
	},
127
	splitByMany: function(seperateBy, joinBy = null){
128
		var sepTextStr = this;
129
		return sepTextStr.seperateByMany(seperateBy, joinBy).removeMultiSpaces().split(" ");
130
	},
131
	
132
	
133
	// convert string into array/vector
134
	splitArray: function(splitBy, trim = false, delBlanks = false){
135
		var str = this;
136
		
137
		// split and return if 0 length
138
		var arr = str.split(splitBy);
139
		var len = arr.length;
140
		if (len == 1 && (String(arr[0]).replace(UB.regex.Trim, "")) == "") {
141
			return [];
142
		}
143
		
144
		// return array of strings
145
		if (trim || delBlanks) {
146
			var newArr = [];
147
			
148
			// convert all slots to numbers if wanted
149
			var ns = 0;
150
			for (var n = 0;n<len;n++){
151
				
152
				// load string value
153
				str = trim ? String(arr[n]).trim() : String(arr[n]);
154
				
155
				// save if non blank
156
				if (!delBlanks || str != ""){
157
					
158
					// save as string
159
					newArr[ns++] = str;
160
				}
161
			}
162
			return newArr;
163
		}
164
		
165
		// quickly return split array
166
		return arr;
167
	},
168
	splitNumberArray: function(seperator, delBlanks = false, valueIfNaN = 0){
169
		var str = this;
170
		
171
		// split and return if 0 length
172
		var arr = str.split(seperator);
173
		var len = arr.length;
174
		var vec = [];
175
		if (len == 1 && (String(arr[0]).replace(UB.regex.Trim, "")) == "") {
176
			return vec;
177
		}
178
		
179
		// convert all slots to numbers if wanted
180
		var ns = 0;
181
		for (var n = 0;n<len;n++){
182
			
183
			// load string value
184
			str = String(arr[n]).replace(UB.regex.Trim, "");
185
			
186
			// save if non blank
187
			if (!delBlanks || str != ""){
188
				
189
				// save as number
190
				var num = Number(str);
191
				if (num != num) {
192
					num = valueIfNaN;
193
				}
194
				vec[ns++] = num;
195
			}
196
		}
197
		return vec;
198
	},
199
	
200
	extractNumbers: function(text, castToInts = true){
201
		var text = this;
202
		var parts = [];
203
		var lastPart = "";
204
		
205
		// per char
206
		for (var c = 0, cl = text.length;c<cl;c++){
207
			var char = text.charCodeAt(c);
208
			
209
			// keep taking continuous numbers
210
			if (Chars.IsDigit(char)) {
0 ignored issues
show
Bug introduced by
The variable Chars seems to be never declared. If this is a global, consider adding a /** global: Chars */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
211
				lastPart += Chars.ToString(char);
212
			}else {
213
				
214
				// add saved numbers if non-number char
215
				if (lastPart.length > 0) {
216
					parts.push(castToInts ? parseInt(lastPart) : lastPart);
217
					lastPart = "";
218
				}
219
			}
220
		}
221
		
222
		if (lastPart.length > 0) {
223
			parts.push(castToInts ? parseInt(lastPart) : lastPart);
224
		}
225
		
226
		return parts;
227
	},
228
	
229
	// parsing
230
	parseProp: function(props, prefix = null, commentChar = "'", trueVal = "True", falseVal = "False"){
231
		var line = this;
232
		
233
		// check if has prop
234
		var words = line.split(" ");
235
		var propName = words[0].trim();
236
		if (propName != "" && words[1] == '=') {
237
			if (prefix != null) {
238
				propName = prefix + "." + propName;
239
			}
240
			
241
			// if string
242
			if (line.indexOf('"') > -1) {
243
				props[propName] = line.between('"', '"');
244
				
245
			}else {
246
				
247
				// if comment
248
				var val, canBeNum;
249
				if (commentChar != null && line.indexOf(commentChar) > -1) {
250
					val = line.afterLast(commentChar).trim();
251
					canBeNum = false;
252
					
253
				// if plain
254
				}else {
255
					val = line.afterFirst('=').trim();
256
					canBeNum = true;
257
				}
258
				
259
				// parse number/bool
260
				if (val == trueVal) {
261
					val = true;
262
				}else if (val == falseVal) {
263
					val = false;
264
				}else if (canBeNum){
265
					val = Number(val);
266
				}
267
				props[propName] = val;
268
			}
269
		}
270
	},
271
	parseVaue: function(){
272
		var value = this;
273
		if (value == "true") {
274
			return true;
275
		}else if (value == "false") {
276
			return false;
277
		}else if (value == "null") {
278
			return null;
279
		}else if (value.isNumeric()) {
280
			return parseFloat(value);
281
		}
282
		return value;
283
	},
284
	parseBool: function(){
285
		var value = this;
286
		
287
		// check quickly .. y/n
288
		if (value == null || value == "n" || value == "N") {
289
			return false;
290
		}
291
		if (value == "1" || value == "y" || value == "Y") {
292
			return true;
293
		}
294
		
295
		// check slowly .. true/false
296
		var valueLower = value.toLowerCase();
297
		return (valueLower == "true" || valueLower == "yes");
298
	},
299
	none:null
300
};
301
302
// register funcs
303
UB.registerFuncs(String.prototype, stringFuncs);
304